What is to-vfile?
The 'to-vfile' npm package is a utility for working with virtual files in Node.js. It allows you to create, read, write, and manipulate file objects in memory, which can be particularly useful for processing files in a pipeline without needing to interact with the filesystem directly.
What are to-vfile's main functionalities?
Creating a virtual file
This feature allows you to create a virtual file object with specified path and contents. The file object can then be manipulated in memory.
const vfile = require('to-vfile');
const file = vfile({ path: 'example.txt', contents: 'Hello, world!' });
console.log(file);
Reading a file from the filesystem
This feature allows you to read a file from the filesystem into a virtual file object. The contents of the file are loaded into memory, and you can manipulate the file object as needed.
const vfile = require('to-vfile');
vfile.read('example.txt', 'utf8', (err, file) => {
if (err) throw err;
console.log(file);
});
Writing a virtual file to the filesystem
This feature allows you to write a virtual file object to the filesystem. The contents of the virtual file are saved to the specified path.
const vfile = require('to-vfile');
const file = vfile({ path: 'example.txt', contents: 'Hello, world!' });
vfile.write(file, err => {
if (err) throw err;
console.log('File written!');
});
Other packages similar to to-vfile
vinyl
Vinyl is a similar package that provides a virtual file format. It is commonly used in the Gulp ecosystem for handling file transformations in memory. Compared to 'to-vfile', Vinyl is more focused on stream-based workflows and integrates tightly with Gulp.
memfs
Memfs is a package that provides an in-memory filesystem. It allows you to perform filesystem operations in memory, which can be useful for testing and mocking. While 'to-vfile' focuses on individual file objects, Memfs provides a broader filesystem abstraction.
to-vfile
Create a vfile from a file-path.
Installation
npm:
npm install to-vfile
to-vfile is also available for bower,
component, and duo,
and as an AMD, CommonJS, and globals module, uncompressed and compressed.
Note: browser-builds do not include read
and readSync
.
Usage
var toVFile = require('to-vfile');
var file = toVFile('./readme.md');
toVFile.read('.git/HEAD', function (err, file) {
if (err) throw err;
console.log(file);
});
var file = toVFile.readSync('.gitignore')
API
toVFile(filePath)
Create a virtual file from filePath
.
Signatures
file = toVFile(filePath)
.
Parameters
filePath
(string
) — Path to a (possibly non-existent) file;
Returns
vfile
— Instance.
toVFile.read(filePath, done)
Create a virtual file from filePath
and fill it with the actual contents
at filePath
.
Signatures
toVFile.read(filePath, callback)
.
Parameters
Returns
vfile
— Instance.
function done(err, vfile)
Callback.
Signatures
function done(Error, null)
;function done(null, VFile)
.
Parameters
err
(Error
) — Error from reading filePath
;vfile
(VFile
) — Virtual file.
toVFile.readSync(filePath)
Create a virtual file from filePath
and fill it with the actual contents at
filePath
(synchroneously).
Signatures
toVFile.read(filePath, callback)
.
Parameters
filePath
(string
) — Path to a (possibly non-existent) file;
Returns
vfile
— Instance.
Throw
Error
— When reading filePath
fails.
License
MIT © Titus Wormer